package com.feiluan.service;

import com.feiluan.util.SignUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class HaierService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final static String BASE_URL = "https://uws.haier.net";
    private final static String CLIENT_ID = "15f0a99470ea8611340e8e6b660040be";
    private final static String CLIENT_SECRET = "85848be90726dd332fc34d2bdda87e2e";
    private final static String APP_KEY = "85848be90726dd332fc34d2bdda87e2e";
    private final static String APP_ID = "SV-ZNJJKZ348-0000";

    @Autowired
    private BasicService basicService;

    @Resource
    SignUtils signUtils;

    public ResponseEntity<String> postLogin() {
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("loginName", "18611426141");
        requestBody.add("password", "123456aa");
        requestBody.add("systemId", "SV-ZNJJKZ348-0000");
        requestBody.add("responseType", "code");
        requestBody.add("scope", "0");
        requestBody.add("redirectUri", "www.baidu.com");
        requestBody.add("loginType", "0");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        String uri = "/oauth/v1/login";
        ResponseEntity<String> result = basicService.postProxy(BASE_URL + uri, headers,
                requestBody, String.class);
        return result;
    }

    public ResponseEntity<String> postToken() {
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("client_id", CLIENT_ID);
        requestBody.add("client_secret", CLIENT_SECRET);
        requestBody.add("redirect_uri", "www.baidu.com");
        requestBody.add("grant_type", "authorization_code");
        requestBody.add("scope", "0");
        JsonObject jsonObject = new Gson().fromJson(this.postLogin().getBody(), JsonObject.class);
        requestBody.add("code", jsonObject.get("code").getAsString());

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        String uri = "/oauth/v1/token";
        ResponseEntity<String> result = basicService.postProxy(BASE_URL + uri, headers, requestBody, String.class);
        return result;
    }


    public ResponseEntity<String> postTokenInfo() {
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        JsonObject jsonObject = new Gson().fromJson(this.postToken().getBody(), JsonObject.class);
        requestBody.add("access_token", jsonObject.get("access_token").getAsString());

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        String uri = "/oauth/2.0/tokeninfo";
        ResponseEntity<String> result = basicService.postProxy(BASE_URL + uri, headers, requestBody, String.class);
        return result;
    }

    public ResponseEntity<String> getDeviceinfos() {

        String uri = "/uds/v1/protected/deviceinfos";
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=UTF-8");
        headers.add("Content-Encoding", "utf-8");
        headers.add("timezone", "Asia/Shanghai");
        headers.add("language", "zh-cn");

        headers.add("appVersion", "1.10");
        headers.add("sequenceId", String.valueOf(System.currentTimeMillis()));

        headers.add("appId", APP_ID);
        headers.add("clientId", CLIENT_ID);
        headers.add("appKey", APP_KEY);

        JsonObject jsonObject = new Gson().fromJson(this.postToken().getBody(), JsonObject.class);
        String access_token = jsonObject.get("access_token").getAsString();

        headers.add("accessToken", access_token);

        String timestamp = String.valueOf(System.currentTimeMillis());
        String body = "";
        headers.add("timestamp", timestamp);
        String sign = signUtils.getSign(APP_ID, APP_KEY, timestamp, body, BASE_URL + uri);
        headers.add("sign", sign);

        ResponseEntity<String> result = basicService.getProxy(BASE_URL + uri, headers, String.class);
        return result;
    }

    public Map<String, String> getSign(String body, String uri) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        if (!uri.contains(BASE_URL)) {
            uri = BASE_URL + uri;
        }
        String sign = signUtils.getSign(APP_ID, APP_KEY, timestamp, body, BASE_URL + uri);
        Map<String, String> map = new HashMap<>();
        map.put("timestamp", timestamp);
        map.put("sign", sign);
        JsonObject jsonObject = new Gson().fromJson(this.postToken().getBody(), JsonObject.class);
        String access_token = jsonObject.get("access_token").getAsString();
        map.put("accessToken", access_token);
        map.put("sequenceId", String.valueOf(System.currentTimeMillis()));
        return map;
    }


    private HttpHeaders calcCommonHeader(String timestamp, String body, String url) {
        HttpHeaders headers = new HttpHeaders();

        headers.add("Content-Type", "application/json;charset=UTF-8");
        headers.add("Content-Encoding", "utf-8");
        headers.add("timezone", "Asia/Shanghai");
        headers.add("language", "zh-cn");

        headers.add("appVersion", "1.10");
        headers.add("sequenceId", String.valueOf(System.currentTimeMillis()));

        headers.add("appId", APP_ID);
        headers.add("clientId", CLIENT_ID);
        headers.add("appKey", APP_KEY);

        JsonObject jsonObject = new Gson().fromJson(this.postToken().getBody(), JsonObject.class);
        String access_token = jsonObject.get("access_token").getAsString();
        headers.add("accessToken", access_token);

        headers.add("timestamp", timestamp);
        String sign = signUtils.getSign(APP_ID, APP_KEY, timestamp, body, url);

        headers.add("sign", sign);

        return headers;

    }

    public ResponseEntity<String> postPropertyWrite(String body) {

//        String APP_ID = "SV-ZNJJKZ348-0000";
        String uri = "/udse/v1/propertyWrite";
        String url = BASE_URL + uri;
        String timestamp = String.valueOf(System.currentTimeMillis());

        HttpHeaders headers = this.calcCommonHeader(timestamp, body, url);

        ResponseEntity<String> result = basicService.postProxy(BASE_URL + uri, headers, body, String.class);
        return result;
    }


//    public ResponseEntity<String> postPropertyWrite(String jsonParam) {
//
////        String APP_ID = "SV-ZNJJKZ348-0000";
//        String uri = "/udse/v1/propertyWrite";
//         HttpHeaders headers = new HttpHeaders();
//        headers.add("Content-Type", "application/json;charset=UTF-8");
//        headers.add("Content-Encoding", "utf-8");
//        headers.add("timezone", "Asia/Shanghai");
//        headers.add("language", "zh-cn");
//
//        headers.add("appVersion", "1.10");
//        headers.add("sequenceId", String.valueOf(System.currentTimeMillis()));
//
//        headers.add("appId", APP_ID);
//        headers.add("clientId", CLIENT_ID);
//        headers.add("appKey", APP_KEY);
//
//        JsonObject jsonObject = new Gson().fromJson(this.postToken().getBody(), JsonObject.class);
//        String access_token = jsonObject.get("access_token").getAsString();
//
//        headers.add("accessToken", access_token);
//
//        String timestamp = String.valueOf(System.currentTimeMillis());
//
//        headers.add("timestamp", timestamp);
//        String sign = signUtils.getSign(APP_ID, APP_KEY, timestamp, jsonParam, BASE_URL + uri);
//        System.out.println("sign :" + sign);
//        headers.add("sign", sign);
//
//        logger.info("APP_ID is " + APP_ID);
//        logger.info("APP_KEY is " + APP_KEY);
//        logger.info("timestamp " + timestamp);
//        logger.info("body is " + jsonParam);
//        logger.info("BASE_URL + uri is " + BASE_URL + uri);
//        logger.info("sign " + sign);
//        logger.info("accessToken " + access_token);
//
//        ResponseEntity<String> result = basicService.postProxy(BASE_URL + uri, headers, jsonParam, String.class);
//        return result;
//    }

    public static void main(String[] args) {
        HaierService haierService = new HaierService();

        haierService.getSign("", "/udse/v1/propertyWrite");
    }


}
